﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Day
{
    /// <summary>
    /// 汇总信息
    /// </summary>
    public class OperatorRealtimeTransaction
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        /// <summary>
        /// 正被查询的交易员
        /// </summary>
        public Models.Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易日供页面显示调用
        /// </summary>
        public DateTime Pdate { get; set; }

        /// <summary>
        /// 横线日期供页面显示调用
        /// </summary>
        public string HPdate { get; set; }

        public string QueryDate { get; set; }

        /// <summary>
        /// 当日交易数据
        /// </summary>
        public Models.View.OperatorDayChange OperatorDayChange { get; set; }

        /// <summary>
        /// 交易员周均值数据
        /// </summary>
        public Models.View.OperatorAvgWeek OperatorAvgWeek { get; set; }

        /// <summary>
        /// 五日均值数据
        /// </summary>
        public Models.View.OperatorFiveDayAvg OperatorFiveDayAvg { get; set; }

        /// <summary>
        /// 当月均值数据
        /// </summary>
        public Models.View.OperatorMonthAvg OperatorMonthAvg { get; set; }

        /// <summary>
        /// 小组均值数据
        /// </summary>
        public Models.Day.GroupAvgDay GroupDayAvg { get; set; }

        /// <summary>
        /// 公司均值数据
        /// </summary>
        public Models.Day.CompanyAvgDay CompanyAvgDay { get; set; }

        /// <summary>
        /// 同批次交易员均值数据
        /// </summary>
        public Models.Day.BatchAvgDay BatchAvgDay { get; set; }

        /// <summary>
        /// 交易员周累计数据
        /// </summary>
        public Models.View.OperatorWeekSum OperatorWeekSum { get; set; }

        /// <summary>
        /// 交易员月累计数据
        /// </summary>
        public Models.View.OperatorMonthSum OperatorMonthSum { get; set; }

        /// <summary>
        /// 交易员年累计数据
        /// </summary>
        public Models.View.OperatorYearSum OperatorYearSum { get; set; }

        /// <summary>
        /// 交易员周预期数据
        /// </summary>
        public Models.View.OperatorWeekExpected OperatorWeekExpected { get; set; }

        /// <summary>
        /// 交易员状态
        /// </summary>
        public Models.Sys._OperatorStatus Status { get; set; }

        public string StatusString { get; set; }

        /// <summary>
        /// 页面JS脚本
        /// </summary>
        public string JS { get; set; }


        #region 定义一些使用到的接口
        private Interface.Sys.IOperator IOperator = null;
        private Interface.View.IOperatorDayChange IOperatorDayChange = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.View.IOperatorAvgWeek IOperatorAvgWeek = null;
        private Interface.View.IOperatorFiveDayAvg IOperatorFiveDayAvg = null;
        private Interface.View.IOperatorMonthAvg IOperatorMonthAvg = null;
        private Interface.Sys.IDayGroupOperators IDayGroupOperators = null;
        private Interface.Day.IGroupAvgDay IGroupAvgDay = null;
        private Interface.Day.ICompanyAvgDay ICompanyAvgDay = null;
        private Interface.Day.IBatchAvgDay IBatchAvgDay = null;
        private Interface.View.IOperatorWeekSum IOperatorWeekSum = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.View.IOperatorYearSum IOperatorYearSum = null;
        private Interface.View.IOperatorWeekExpected IOperatorWeekExpected = null;
        private Interface.Sys.IOperatorStatus IOperatorStatus = null;
        private Interface.View.IOperatorDayTradeInfo IOperatorDayTradeInfo = null;
        private Interface.View.IOperatorDayProductInfo IOperatorDayProductInfo = null;
        #endregion



        public OperatorRealtimeTransaction(int uid, string queryDate, DB.DBManager db)
        {
            InitInterface(db);

            this.QueryDate = queryDate;

            this.Operator = IOperator.GetModel(uid);

            string _year = queryDate.ToString().Substring(0, 4);
            string _month = queryDate.ToString().Substring(4, 2);
            string _day = queryDate.ToString().Substring(6, 2);
            string _pdate = _year + "-" + _month + "-" + _day;
            this.Pdate = Convert.ToDateTime(_pdate);
            this.HPdate = _pdate;
            Models.Sys.TradeHistory th = ITradeHistory.GetSettlementModel(queryDate);
            this.OperatorDayChange = IOperatorDayChange.GetOperatorDayChange(this.Operator.Id, th.Id);

            this.OperatorAvgWeek = IOperatorAvgWeek.GetOperatorAvgWeek(this.Operator.Id, th.YPdate, th.WPdate);//查询交易员周均值


            this.OperatorFiveDayAvg = IOperatorFiveDayAvg.GetOperatorFiveDayAvg(this.Operator.Id, th.Id);//五日均值

            this.OperatorMonthAvg = IOperatorMonthAvg.GetOperatorMonthAvg(this.Operator.Id, th.MPdate, th.YPdate);//当月均值

            int groupid = IDayGroupOperators.GetGroupId(this.Operator.Id, this.Pdate);
            this.GroupDayAvg = IGroupAvgDay.GetGroupAvgDay(groupid, th.Id);//查询小组均值

            this.CompanyAvgDay = ICompanyAvgDay.GetCompanyAvgDay(th.Id);//查询公司均值

            this.BatchAvgDay = IBatchAvgDay.GetBatchAvgDay(this.Operator.Id, th.Id);//查询同批次交易员均值

            this.OperatorWeekSum = IOperatorWeekSum.GetOperatorWeekSum(this.Operator.Id, th.YPdate, th.MPdate, th.WPdate);//查询交易员周累计

            this.OperatorMonthSum = IOperatorMonthSum.GetOperatorMonthSum(this.Operator.Id, th.MPdate, th.YPdate);//查询交易员月累计


            this.OperatorYearSum = IOperatorYearSum.GetOperatorYearSum(this.Operator.Id, th.YPdate);//查询交易员年累计

            this.OperatorWeekExpected = IOperatorWeekExpected.GetOperatorWeekExpected(this.Operator.Id, th.WPdate, th.YPdate);//查询交易员周预期

            Models.Sys._OperatorStatus status = IOperatorStatus.GetOperatorStatus(this.OperatorDayChange.Profit);
            this.Status = status;

            Models.View.OperatorDayTradeInfo info = IOperatorDayTradeInfo.GetOperatorDayTradeInfo(th.Id, this.Operator.Id, "profit");
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.GetTodayBenefitString(info);

            List<Models.View.OperatorDayProductInfo> productInfos = IOperatorDayProductInfo.GetOperatorDayProductInfoIgnoreDirection(th.Id, this.Operator.Id);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.ProductPie(productInfos);

            switch (this.Status)
            {
                case Models.Sys._OperatorStatus.Good:
                    this.StatusString = "较好";
                    break;
                case Models.Sys._OperatorStatus.MostPoor:
                    this.StatusString = "极差";
                    break;
                case Models.Sys._OperatorStatus.Poor:
                    this.StatusString = "较差";
                    break;
                case Models.Sys._OperatorStatus.VeryGood:
                    this.StatusString = "非常好";
                    break;
                case Models.Sys._OperatorStatus.VeryPoor:
                    this.StatusString = "非常差";
                    break;
            }

            DisposeInterface();
        }


        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IOperator = new Service.Sys.ServiceOperator(db);
            IOperatorDayChange = new Service.View.ServiceOperatorDayChange(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IOperatorAvgWeek = new Service.View.ServiceOperatorAvgWeek(db);
            IOperatorFiveDayAvg = new Service.View.ServiceOperatorFiveDayAvg(db);
            IOperatorMonthAvg = new Service.View.ServiceOperatorMonthAvg(db);
            IDayGroupOperators = new Service.Sys.ServiceDayGroupOperators(db);
            IGroupAvgDay = new Service.Day.ServiceGroupAvgDay(db);
            ICompanyAvgDay = new Service.Day.ServiceCompanyAvgDay(db);
            IBatchAvgDay = new Service.Day.ServiceBatchAvgDay(db);
            IOperatorWeekSum = new Service.View.ServiceOperatorWeekSum(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            IOperatorYearSum = new Service.View.ServiceOperatorYearSum(db);
            IOperatorWeekExpected = new Service.View.ServiceOperatorWeekExpected(db);
            IOperatorStatus = new Service.Sys.ServiceOperatorStatus(db);
            IOperatorDayTradeInfo = new Service.View.ServiceOperatorDayTradeInfo(db);
            IOperatorDayProductInfo = new Service.View.ServiceOperatorDayProductInfo(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IOperator = null;
            IOperatorDayChange = null;
            ITradeHistory = null;
            IOperatorAvgWeek = null;
            IOperatorFiveDayAvg = null;
            IOperatorMonthAvg = null;
            IDayGroupOperators = null;
            IGroupAvgDay = null;
            ICompanyAvgDay = null;
            IBatchAvgDay = null;
            IOperatorWeekSum = null;
            IOperatorMonthSum = null;
            IOperatorYearSum = null;
            IOperatorWeekExpected = null;
            IOperatorStatus = null;
            IOperatorDayTradeInfo = null;
            IOperatorDayProductInfo = null;
            #endregion
        }
    }
}
